package net.awpspace.mobile.management.house;

import javax.microedition.midlet.MIDlet;

import net.awpspace.mobile.form.BaseForm;
import net.awpspace.mobile.global.Utils;

import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.spinner.Spinner;

public class DetailsMemberViewer extends BaseForm {

	private HouseMember member;

	private String EDIT = "Edit";
	private String SAVE = "Save";
	private Command edit;
	private Command save;

	private TextField tname;
	private TextField taddress;
	private TextField ticard;
	private RadioButton male;
	private RadioButton female;
	private TextField sex;
	private Spinner day, month, year;
	private Label lbdate;
	private Container con;
	private Container date;

	public DetailsMemberViewer(MIDlet app, BaseForm parent, HouseMember hm) {
		super(app, parent);
		setTitle("Member Details");
		this.member = hm;

		createComponent();
		createCommand();
	}

	public void createComponent() {

		con = new Container(new BoxLayout(BoxLayout.Y_AXIS));
		Label lb = new Label("Thông tin chi tiết");

		Label lbname = new Label("Họ tên");
		tname = new TextField(this.member.getName());

		Container consex = new Container(new GridLayout(2, 2));
		Label lbsex = new Label("Giới tính :");
		sex = new TextField(this.member.getSex());
		male = new RadioButton("Nam");
		female = new RadioButton("Nữ");
		if (member.getSex().equals("nam"))
			male.setSelected(true);
		else
			female.setSelected(true);
		ButtonGroup bg = new ButtonGroup();
		bg.add(male);
		bg.add(female);
		consex.addComponent(lbsex);
		consex.addComponent(sex);
		consex.addComponent(male);
		consex.addComponent(female);

		lbdate = new Label("Ngày sinh: " + member.getDate().toString());

		date = new Container(new GridLayout(1, 3));
		date.getStyle().setBorder(Border.createLineBorder(1), true);
		day = Spinner.create(1, 32, 2, 1);
		month = Spinner.create(1, 13, 2, 1);
		year = Spinner.create(1940, 2014, 1991, 1);
		day.setValue(new Integer(member.getDate().getDay()));
		month.setValue(new Integer(member.getDate().getMonth()));
		year.setValue(new Integer(member.getDate().getYear()));
		date.addComponent(day);
		date.addComponent(month);
		date.addComponent(year);

		Label lbicard = new Label("Chứng minh thư");
		ticard = new TextField(this.member.getIcard());
		ticard.setConstraint(TextField.NUMERIC);
		ticard.setMaxSize(10);

		Label lbaddress = new Label("Địa chỉ");
		taddress = new TextField(this.member.getAddress());

		con.addComponent(lb);
		con.addComponent(lbname);
		con.addComponent(tname);
		con.addComponent(consex);
		con.addComponent(lbdate);
		// con.addComponent(date);
		con.addComponent(lbicard);
		con.addComponent(ticard);
		con.addComponent(lbaddress);
		con.addComponent(taddress);

		enable(false);

		addComponent(con);

	}

	public void enable(boolean b) {
		tname.setEditable(b);
		sex.setEditable(b);
		sex.setVisible(!b);
		male.setVisible(b);
		female.setVisible(b);
		// day.setEnabled(b);
		// day.setFocusable(true);
		// month.setEnabled(b);
		// month.setFocusable(true);
		// year.setEnabled(b);
		// year.setFocusable(true);
		if (b) {
			lbdate.setText("Ngày sinh");
			int t = con.getComponentIndex(lbdate);
			con.addComponent(t + 1, date);
		}
		ticard.setEditable(b);
		taddress.setEditable(b);
	}

	public void createCommand() {
		edit = new Command(EDIT);
		save = new Command(SAVE);
		addCommand(save);
		addCommand(edit);

	}

	public void save() {
		member.setName(tname.getText());
		String sex = "";
		if (male.isSelected())
			sex = "nam";
		else if (female.isSelected())
			sex = "nữ";
		member.setSex(sex);
		String d = String.valueOf(this.day.getSelectedItem());
		String m = String.valueOf(this.month.getSelectedItem());
		String y = String.valueOf(this.year.getSelectedItem());
		member.setDate(new AwpDate(d + "/" + m + "/" + y));
		member.setIcard(ticard.getText());
		member.setAddress(taddress.getText());
		try {
			Persistence.getInstance().updateHouseMember(member);
			AudioManager.getInstance().play(AudioManager.SUCCESS);
			Utils.getInstance().alert("Cập nhật thành công");
		} catch (Exception e) {
			// TODO: handle exception
			AudioManager.getInstance().play(AudioManager.FAILURE);
			Utils.getInstance().alert("Hãy điền đầy đủ thông tin");
		}

	}

	protected void processCommand(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getCommand().equals(save)) {
			save();
		} else if (e.getCommand().equals(edit)) {
			AudioManager.getInstance().play(AudioManager.BUTTON);
			enable(true);
		}
	}

}
